package com.company.ecis.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.ecis.entity.BindingCard;
import com.company.ecis.mapper.BindingCardMapper;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.ecis.entity.BankCard;
import com.company.ecis.entity.BindingCard;
import com.company.ecis.entity.Userinfo;
import com.company.ecis.mapper.BankCardMapper;
import com.company.ecis.mapper.BindingCardMapper;
import com.company.ecis.mapper.UserinfoMapper;

import com.company.ecis.service.BindingCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-04
 */
@Service
public class BindingCardServiceImpl extends ServiceImpl<BindingCardMapper, BindingCard> implements BindingCardService {

    @Autowired
    BankCardMapper bankCardMapper;

    @Autowired
    BindingCardMapper bindingCardMapper;

    @Autowired
    UserinfoMapper userinfoMapper;

    private BindingCard bindingCard;
    private Userinfo userinfo;


    @Override
    public List<BindingCard> selectCardsByUsername(String username) {
        QueryWrapper<BindingCard> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",username);
        return bindingCardMapper.selectList(wrapper);
    }




    @Override
    public String bindCard( String password, String cardId, HttpSession session) {

//        Userinfo userinfo1 = (Userinfo) session.getAttribute(String.valueOf(userId));
//        System.out.println(userinfo1);
//        List<BindingCard> cardList= bindingCardService.selectCardsByUsername(userInfo.getUsername());

        Userinfo userinfo = (Userinfo) session.getAttribute("用户信息");
        System.out.println(userinfo);
        String username = userinfo.getUsername();

        //判断参数是否为空
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(cardId)) {
            throw new RuntimeException("参数为空");
        }

        //查看是否存在卡号
        LambdaQueryWrapper<BankCard> qw = new LambdaQueryWrapper<>();
        qw.eq(BankCard::getId, cardId);
        BankCard Card = bankCardMapper.selectOne(qw);
        if (Objects.isNull(Card)) {
            throw new RuntimeException("不存在此卡");
        }

        //判断是否已经绑定
        LambdaQueryWrapper<BindingCard> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(BindingCard::getUserId, username);
        qw2.and(qr -> qr.eq(BindingCard::getId, cardId));
        BindingCard bindCard = bindingCardMapper.selectOne(qw2);
        if (!Objects.isNull(bindCard)) {
            throw new RuntimeException("已经绑定过了");
        }

        //判断绑定密码是否正确
        LambdaQueryWrapper<Userinfo> qw3 = new LambdaQueryWrapper<>();
        qw3.eq(Userinfo::getUsername, username);
        qw3.and(qr -> qr.eq(Userinfo::getPassword, SecureUtil.md5(userinfo.getPassword())));
        Userinfo finaluserinfo = userinfoMapper.selectOne(qw3);
        if (Objects.isNull(finaluserinfo)) {
            throw new RuntimeException("密码错误");
        }

        //开始绑定
        BindingCard bc = new BindingCard();
        bc.setId(cardId);
        bc.setUserId(username);
        int a = bindingCardMapper.insert(bc);

        //返回数据
        JSONObject jsonObject = JSONUtil.createObj();
        if (a < 1) {
            jsonObject.set("code", 502).set("msg", "绑定失败");
            return JSONUtil.toJsonStr(jsonObject);
        }
        jsonObject.set("code", 200).set("msg", "绑定成功");
        return JSONUtil.toJsonStr(jsonObject);
    }

}
